package com.example.receiptocr

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.*
//noinspection UsingMaterialAndMaterial3Libraries
import androidx.compose.material.BottomNavigation
//noinspection UsingMaterialAndMaterial3Libraries
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Person
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.PhotoCamera
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.navigation.NavController
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
//import androidx.navigation.navType.navArgument
import com.example.receiptocr.profile.ProfileScreen
import com.example.receiptocr.viewModels.UserProfileViewModel
import com.example.receiptocr.home.HomeScreen
import com.example.receiptocr.search.SearchScreen
import com.example.receiptocr.ocr.CameraScreen
import com.example.receiptocr.ocr.OCRResultScreen
import com.example.receiptocr.AIChat.AIChatScreen
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@Composable
fun MainScreen(navController: NavController) {
    var selectedTab by remember { mutableStateOf(0) }
    val userProfileViewModel: UserProfileViewModel = hiltViewModel()
    val nestedNavController = rememberNavController()
    
    Scaffold(
        bottomBar = {
            BottomNavigationBar(selectedTab, onTabSelected = { selectedTab = it })
        }
    ) {
        when (selectedTab) {
            0 -> {
                NavHost(navController = nestedNavController, startDestination = "home") {
                    composable("home") {
                        HomeScreen(
                            navController = nestedNavController,
                            onNavigateToCamera = {
                                selectedTab = 2
                            }
                        )
                    }
//                    composable(
//                        route = "receipt_detail/{receiptId}",
//                        arguments = listOf(
////                            navArgument("receiptId") { type = NavType.LongType }
//                        )
//                    ) { backStackEntry ->
//                        val receiptId = backStackEntry.arguments?.getLong("receiptId") ?: return@composable
//                        OCRResultScreen(
//                            onNavigateBack = { nestedNavController.popBackStack() },
//                            onSaveAndReturn = { nestedNavController.popBackStack() }
//                        )
//                    }
                }
            }
            1 -> SearchScreen(navController)
            2 -> {
                NavHost(navController = nestedNavController, startDestination = "ocr") {
                    composable("ocr") {
                        CameraScreen(
                            navController = nestedNavController
                        )
                    }
                }
            }
            3 -> {
                NavHost(navController = nestedNavController, startDestination = "aiChat") {
                    composable("aiChat") {
                        AIChatScreen(navController = nestedNavController)
                    }
                }
            }
            4 -> {
                NavHost(navController = nestedNavController, startDestination = "profileScreen") {
                    composable("profileScreen") {
                        ProfileScreen(navController = nestedNavController)
                    }
                }
            }
        }
    }
}
@Composable
fun BottomNavigationBar(selectedTab: Int, onTabSelected: (Int) -> Unit) {
    BottomNavigation(
        modifier = Modifier.fillMaxWidth(),
//        backgroundColor = Color(0xFFf1f1f1),  // 设置背景为灰色
        backgroundColor = MaterialTheme.colorScheme.surface
    ) {
        BottomNavigationItem(
            selected = selectedTab == 0,
            onClick = { onTabSelected(0) },
            label = { Text("首页", color = MaterialTheme.colorScheme.onSurface) },
            icon = {
                Icon(
                    Icons.Default.Home,
                    contentDescription = "首页",
                    tint = if (selectedTab == 0) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface // 选中时为绿色，否则为默认颜色
                )
            }
        )
        BottomNavigationItem(
            selected = selectedTab == 1,
            onClick = { onTabSelected(1) },
            label = { Text("搜索", color = MaterialTheme.colorScheme.onSurface) },
            icon = {
                Icon(
                    Icons.Default.Search,
                    contentDescription = "搜索",
                    tint = if (selectedTab == 1) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface
                )
            }
        )
        BottomNavigationItem(
            selected = selectedTab == 2,
            onClick = { onTabSelected(2) },
            label = { Text("识别", color = MaterialTheme.colorScheme.onSurface) },
            icon = {
                Icon(
                    Icons.Default.PhotoCamera,
                    contentDescription = "识别",
                    tint = if (selectedTab == 2) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface
                )
            }
        )
        BottomNavigationItem(
            selected = selectedTab == 3,
            onClick = { onTabSelected(3) },
            label = { Text("AI", color = MaterialTheme.colorScheme.onSurface) },
            icon = {
                Icon(
                    Icons.Default.Star,
                    contentDescription = "AI",
                    tint = if (selectedTab == 3) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface
                )
            }
        )
        BottomNavigationItem(
            selected = selectedTab == 4,
            onClick = { onTabSelected(4) },
            label = { Text("我的", color = MaterialTheme.colorScheme.onSurface) },
            icon = {
                Icon(
                    Icons.Default.Person,
                    contentDescription = "我的",
                    tint = if (selectedTab == 4) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface
                )
            }
        )

    }
}